// pages/serve/serve.js
var utils = require("../../utils/util.js")
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // https: 'https://api.apiopen.top/musicBroadcasting',
    https: '',
    express: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this

    var https = app.globalData.SearchMusic + '?name='


    
    wx.request({
      url: 'https://weixin.qiyehuo.com/api/test',
      // data: { strCallUserCode: 'test', strCallPassword: '123' },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      }, // 设置请求的 header
      success: function(res) {
        var Data = res.data;
        console.log(Data)  

        that.setData({
          listData: Data
        })

      },
      fail: function(res) {

      }

    })

    this.data.https = https






  },
  input: function(event) {
    //console.log(event)
    this.setData({
      expressNu: event.detail.value
    })

    //console.log(this.data.expressNu)

  },
  btnClick: function(event) {

    var express = this.data.expressNu
    var https = this.data.https
    //var express2 = this.data.express
    console.log(this.data.express)
    if (express == null){

      //console.log('kong')
      
      wx.showToast({
        title: '不能为空',
        icon: 'none',
        duration: 1000
      })

    }
    else if(express == 0){

      wx.showToast({
        title: '不能为空',
        icon: 'none',
        duration: 1000
      })
    }
    else{

      var dataUrl = https + express

      //console.log(dataUrl)

      utils.getAjax(dataUrl, '', this.getMusic)
      wx.showLoading({
        title: '玩命搜索中',
      })
     
    }
    // console.log(express)
    // console.log(https)

    // utils.getAjax(https, express, res => {
    //   console.log(res)
    // })
    
   

  },

  getMusic: function(res) {
    
    var expressNu = this.data.expressNu
    //var express = this.data.express

    //console.log(res)
    //console.log(res.data.result)
   
    var audios = []
    
    for (var idx in res.data.result)

     {
      var musicdata = res.data.result[idx]
      //console.log(res.data.result[idx].title)

      var musicName = musicdata.title
      //console.log(musicName)

      if (musicName.length >= 6) {
        
        var musicName = musicName.substring(0,6) + '...'
        
      }

      var temp = {

        author: musicdata.author,//作者名
        musicName: musicName, //音乐名
        audioImg: musicdata.pic, //封面
        musicId: musicdata.songid, //id
        audioUrl: musicdata.url //音频文件

      }

      
      
      audios.push(temp)
      //console.log(audios)
    

    }
    //console.log(audios.length)
    
    //wx.hideLoading()
    var length = audios.length;
    
    //console.log(express)
    //console.log(length)

    this.setData({
      audios:audios
    })

    console.log(expressNu)
    
    if (expressNu == 0 ){
      wx.hideLoading()
      wx.showToast({
        title: '不能为空',
        icon: 'none',
        duration: 1000
      })
    }
    else{
      wx.showToast({
        title: '已为你找到' + length + '歌曲',
        icon: 'none',
        duration: 1000,

      })  
    }
    
    
    

    

    //console.log(url)
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {


  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})